// 用户自定义返佣设置
layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    form.on("submit(service)", function (data) {
        var id = $("#Id").val();
        var services = [];
        $(".service").each(function (index, item) {
            item.value && services.push({
                Id: $(item).attr("id"),
                ProjectManagerId: id,
                ProjectName: $(item).attr("chargeName"),
                ProjectUnit: $(item).attr("chargeUnit"),
                ProjectPrice: item.value,
                ProjectCode: $(item).attr("name")
            });
        });
        if (services.length) {
            $.ajax({
                type: 'POST',
                url: '/TradeCommission/SettingUserTrade/',
                data: {
                    Services: services
                },
                dataType: "json",
                headers: {
                    "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
                },
                success: function (res) {//res为相应体,function为回调函数
                    if (res.ResultCode === 200) {
                        var alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                            layer.closeAll("iframe");
                            //刷新父页面
                            parent.location.reload();
                            top.layer.close(alertIndex);
                        });
                    }
                    else {
                        layer.alert(res.ResultMsg, { icon: 5 });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                }
            });
        } else {
            layer.alert('请添加您要设置的手续费！', { icon: 5 });
        }
        return false;
    });

    form.verify({
        service: function (value, item) { //value：表单的值、item：表单的dom对象
            if (value) {
                if (isNaN(value)) {
                    return '只能录入数字！';
                }
                var tag = $(item).attr('tag');
                var val = parseFloat(value);
                var trade = parseFloat($(item).attr('trade'));
                var charge = parseFloat($(item).attr('charge'));
                
                if (val > charge) {
                    return '直客的' + tag + '手续费不能高于总手续费！';
                }
                if (val < trade) {
                    return '直客的' + tag + '手续费不能低于标准手续费！';
                }
            }
        }
    });


});